package com.talking.activities.main
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityOptionsCompat
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.talking.App
import com.talking.R
import com.talking.activities.AccountActivity
import com.talking.activities.LoginActivity
import com.talking.adapter.MeAdapter
import com.talking.bean.MeBean
import com.talking.databinding.FragmentMeBinding
import com.talking.util.Network
import com.talking.util.Tools

class MeFragment : Fragment() {
    private  val data: MutableList<MeBean> = ArrayList()
    private lateinit var adp: MeAdapter
    private lateinit var launcher: ActivityResultLauncher<Intent>
    private lateinit var binding: FragmentMeBinding
    private lateinit var act: MainActivity
    @SuppressLint("CommitPrefEdits")
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        binding = FragmentMeBinding.inflate(inflater, container, false)
        act= activity as MainActivity
        binding.meAccount.setOnClickListener { launcher.launch(Intent(act,AccountActivity::class.java),ActivityOptionsCompat.makeSceneTransitionAnimation(act, binding.meIcon, "icon")) }
        binding.meList.onItemClickListener = AdapterView.OnItemClickListener { _: AdapterView<*>?, _: View?, i: Int, _: Long ->
            when(data[i].cmd){
                "logout"->{
                    val edit = App.sp.edit()
                    edit.putBoolean("canautologin",false)
                    edit.apply()
                    launcher.launch(Intent(act,LoginActivity::class.java))
                    act.finish()
                }
                "account"->launcher.launch(Intent(act,AccountActivity::class.java),ActivityOptionsCompat.makeSceneTransitionAnimation(act, binding.meIcon, "icon"))
            }
        }
        initList()
        launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
            act.refresh(object:RefreshCallback{
                override fun callback() {
                    refresh()
                }
            })
        }
        refresh()
        return binding.root
    }

    private fun refresh() {
        binding.meNickname.text= App.me.nickname
        binding.meMail.text= App.me.mail
        Glide.with(this).load(Network.BASE_URL+ App.me.iconUrl).apply(Tools.getGlideOption()).placeholder(R.drawable.loading).placeholder(R.drawable.loading).error(R.drawable.loaderr).into(binding.meIcon)
    }


    private fun initList() {
        data.add(MeBean(R.drawable.profile,getString(R.string.my_account),"account"))
        data.add(MeBean(R.drawable.logout,getString(R.string.logout),"logout"))
        adp=MeAdapter(act,data)
        binding.meList.adapter=adp
    }
}